import utils from '../utils'
import axios from 'axios'
// import qs from 'qs'
import app from '../main.js'
let Base64 = require('js-base64').Base64

// const clearRequest = {
//   source: {
//     token: null,
//     cancel: null
//   }
// }
// const CancelToken = axios.CancelToken;
// const source = CancelToken.source();
// // 配置发送请求拦截器
// axios.interceptors.request.use(config => {
//   config.cancelToken = clearRequest.source.token;// 这句很重要
//   return config
// }, error => {
//   return Promise.reject(error)
// })

// const GATEWAY = 'http://api.mh.test.com/'
//测试环境
const GATEWAY = 'http://api.test.miaohui.com/'
//生产环境
// const GATEWAY = '//api.app.miaohui.com/'
// const GATEWAY = '/api/'

const SUCCESS_CODE = 0
const ERROR_CODE = 1
const NEED_LOGIN = 401
const NEED_UPDATE_TOKEN = 403

const HEADER_ACCEPT_TIME = "Accept-Time"
const HEADER_ACCEPT_TOKEN= "Accept-Token"
const HEADER_ACCEPT_SIGN = "Accept-Sign"
const HEADER_ACCEPT_DEVICE = "Accept-Device"
const HEADER_PKG_NAME = "pkg-name"
const HEADER_PKG_IDENTIFY = "pkg-identify"
const HEADER_PKG_VERSION = "pkg-version"



export default {
  async request(api, option, mwsuccess, mwerror, method, loading,skiplogin) {
    var that = this
    if (loading) {
      var requestprogress = new utils.requestprogress();
      requestprogress.show();
    }

    var complete_url = GATEWAY + api;
    let complete_header = {};
    complete_header[HEADER_PKG_NAME] = "com.miaohui.h5"
    complete_header[HEADER_PKG_IDENTIFY] = "miaohui"
    complete_header[HEADER_PKG_VERSION] = "1.0"
    complete_header[HEADER_ACCEPT_TOKEN] = (localStorage.getItem(HEADER_ACCEPT_TOKEN) === null) ? "":localStorage.getItem(HEADER_ACCEPT_TOKEN);
    complete_header[HEADER_ACCEPT_TIME] = utils.formatTime(new Date());
    // complete_header["Access-Control-Allow-Origin"] = true

    var beforesign = "";
    // var options = mwtrim(option)
    var keylist = Object.keys(option).sort();
    for (let i in keylist) {
      beforesign = beforesign + keylist[i] + option[keylist[i]]
    };
    beforesign = beforesign + complete_header[HEADER_ACCEPT_TIME];
    beforesign = complete_header[HEADER_ACCEPT_TOKEN] + beforesign + complete_header[HEADER_ACCEPT_TOKEN];
    beforesign = Base64.encode(beforesign);
    beforesign = sign(beforesign);
    complete_header[HEADER_ACCEPT_SIGN] = beforesign;
    complete_header[HEADER_ACCEPT_DEVICE] = "wap";

    var requestconfig;
    if (method == "GET" || method == "DELETE") {
      requestconfig = {
        method:method,
        headers:complete_header,
        url:complete_url,
        params:option
      };
    } else if (method == "POST") {
      requestconfig = {
        method:method,
        headers:complete_header,
        url:complete_url,
        data:option
      };
    }
    axios(requestconfig).then((response) =>{
      // alert(JSON.stringify(response.config.headers))
      if (loading) {
        requestprogress.hide()
      }
      if (response.status == 200) {
        if (response.data.status == SUCCESS_CODE) {
          mwsuccess(response.data.result.data)
        } else if (response.data.status == NEED_LOGIN) {
          if (skiplogin != "" && skiplogin != null) {
            app.$vuet.getModule(skiplogin).needFetch = false
            app.$pushRouteByJson({name:"login"})
          }

        } else if (response.data.status == NEED_UPDATE_TOKEN) {
          // alert(response.data.result.token)
          localStorage.setItem(HEADER_ACCEPT_TOKEN, response.data.result.token)
          that.request(api, option, mwsuccess, mwerror, method, loading,skiplogin)
        } else if (response.data.status == ERROR_CODE) {
          utils.toast(response.data.message)
        }
      }else {
        if (loading) {
          requestprogress.hide()
        }
        utils.toast("网络连接出错")
        mwerror();
      }

      // console.log(response)       //请求成功返回的数据
    }).catch((error) =>{
      if (loading) {
        requestprogress.hide()
      }
      utils.toast("网络连接出错")
      mwerror();
    })

  }
}

const mwtrim = (option) => {
  var finaloption = {};
  var keys = Object.keys(option)
  for (var i = 0; i < keys.length; i++) {
    var temp = "" + option[keys[i]]
    finaloption[keys[i]] = temp.replace(/(^\s*)|(\s*$)/g, "")
  }
  return finaloption
};

function sign(aP) {
  function aO(d, c) {
    return d << c | d >>> 32 - c
  }

  function aN(i, h) {
    var o, n, m, l, k;
    return m = 2147483648 & i,
      l = 2147483648 & h,
      o = 1073741824 & i,
      n = 1073741824 & h,
      k = (1073741823 & i) + (1073741823 & h),
      o & n ? 2147483648 ^ k ^ m ^ l : o | n ? 1073741824 & k ? 3221225472 ^ k ^ m ^ l : 1073741824 ^ k ^ m ^ l : k ^ m ^ l
  }

  function aM(e, d, f) {
    return e & d | ~e & f
  }

  function aL(e, d, f) {
    return e & f | d & ~f
  }

  function aK(e, d, f) {
    return e ^ d ^ f
  }

  function aJ(e, d, f) {
    return d ^ (e | ~f)
  }

  function aI(b, n, m, l, k, d, c) {
    return b = aN(b, aN(aN(aM(n, m, l), k), c)),
      aN(aO(b, d), n)
  }

  function aH(b, n, m, l, k, e, c) {
    return b = aN(b, aN(aN(aL(n, m, l), k), c)),
      aN(aO(b, e), n)
  }

  function aG(b, n, m, l, k, f, c) {
    return b = aN(b, aN(aN(aK(n, m, l), k), c)),
      aN(aO(b, f), n)
  }

  function aF(b, n, m, l, k, g, c) {
    return b = aN(b, aN(aN(aJ(n, m, l), k), c)),
      aN(aO(b, g), n)
  }

  function aE(s) {
    for (var r, q = s.length, p = q + 8, o = (p - p % 64) / 64, n = 16 * (o + 1), m = new Array(n - 1), l = 0, k = 0; q > k;) {
      r = (k - k % 4) / 4,
        l = k % 4 * 8,
        m[r] = m[r] | s.charCodeAt(k) << l,
        k++
    }
    return r = (k - k % 4) / 4,
      l = k % 4 * 8,
      m[r] = m[r] | 128 << l,
      m[n - 2] = q << 3,
      m[n - 1] = q >>> 29,
      m
  }

  function aD(g) {
    var f, k, i = "", h = "";
    for (k = 0; 3 >= k; k++) {
      f = g >>> 8 * k & 255,
        h = "0" + f.toString(16),
        i += h.substr(h.length - 2, 2)
    }
    return i
  }

  function aC(f) {
    f = f.replace(/\r\n/g, "\n");
    for (var e = "", h = 0; h < f.length; h++) {
      var g = f.charCodeAt(h);
      128 > g ? e += String.fromCharCode(g) : g > 127 && 2048 > g ? (e += String.fromCharCode(g >> 6 | 192),
        e += String.fromCharCode(63 & g | 128)) : (e += String.fromCharCode(g >> 12 | 224),
        e += String.fromCharCode(g >> 6 & 63 | 128),
        e += String.fromCharCode(63 & g | 128))
    }
    return e
  }

  var aB, aA, az, ay, ax, aw, av, au, at, ar = [], aq = 7, ap = 12, ao = 17, an = 22, am = 5, al = 9, ak = 14,
    aj = 20, ai = 4, ah = 11, ag = 16, af = 23, ae = 6, ad = 10, ac = 15, ab = 21;
  for (aP = aC(aP),
         ar = aE(aP),
         aw = 1732584193,
         av = 4023233417,
         au = 2562383102,
         at = 271733878,
         aB = 0; aB < ar.length; aB += 16) {
    aA = aw,
      az = av,
      ay = au,
      ax = at,
      aw = aI(aw, av, au, at, ar[aB + 0], aq, 3614090360),
      at = aI(at, aw, av, au, ar[aB + 1], ap, 3905402710),
      au = aI(au, at, aw, av, ar[aB + 2], ao, 606105819),
      av = aI(av, au, at, aw, ar[aB + 3], an, 3250441966),
      aw = aI(aw, av, au, at, ar[aB + 4], aq, 4118548399),
      at = aI(at, aw, av, au, ar[aB + 5], ap, 1200080426),
      au = aI(au, at, aw, av, ar[aB + 6], ao, 2821735955),
      av = aI(av, au, at, aw, ar[aB + 7], an, 4249261313),
      aw = aI(aw, av, au, at, ar[aB + 8], aq, 1770035416),
      at = aI(at, aw, av, au, ar[aB + 9], ap, 2336552879),
      au = aI(au, at, aw, av, ar[aB + 10], ao, 4294925233),
      av = aI(av, au, at, aw, ar[aB + 11], an, 2304563134),
      aw = aI(aw, av, au, at, ar[aB + 12], aq, 1804603682),
      at = aI(at, aw, av, au, ar[aB + 13], ap, 4254626195),
      au = aI(au, at, aw, av, ar[aB + 14], ao, 2792965006),
      av = aI(av, au, at, aw, ar[aB + 15], an, 1236535329),
      aw = aH(aw, av, au, at, ar[aB + 1], am, 4129170786),
      at = aH(at, aw, av, au, ar[aB + 6], al, 3225465664),
      au = aH(au, at, aw, av, ar[aB + 11], ak, 643717713),
      av = aH(av, au, at, aw, ar[aB + 0], aj, 3921069994),
      aw = aH(aw, av, au, at, ar[aB + 5], am, 3593408605),
      at = aH(at, aw, av, au, ar[aB + 10], al, 38016083),
      au = aH(au, at, aw, av, ar[aB + 15], ak, 3634488961),
      av = aH(av, au, at, aw, ar[aB + 4], aj, 3889429448),
      aw = aH(aw, av, au, at, ar[aB + 9], am, 568446438),
      at = aH(at, aw, av, au, ar[aB + 14], al, 3275163606),
      au = aH(au, at, aw, av, ar[aB + 3], ak, 4107603335),
      av = aH(av, au, at, aw, ar[aB + 8], aj, 1163531501),
      aw = aH(aw, av, au, at, ar[aB + 13], am, 2850285829),
      at = aH(at, aw, av, au, ar[aB + 2], al, 4243563512),
      au = aH(au, at, aw, av, ar[aB + 7], ak, 1735328473),
      av = aH(av, au, at, aw, ar[aB + 12], aj, 2368359562),
      aw = aG(aw, av, au, at, ar[aB + 5], ai, 4294588738),
      at = aG(at, aw, av, au, ar[aB + 8], ah, 2272392833),
      au = aG(au, at, aw, av, ar[aB + 11], ag, 1839030562),
      av = aG(av, au, at, aw, ar[aB + 14], af, 4259657740),
      aw = aG(aw, av, au, at, ar[aB + 1], ai, 2763975236),
      at = aG(at, aw, av, au, ar[aB + 4], ah, 1272893353),
      au = aG(au, at, aw, av, ar[aB + 7], ag, 4139469664),
      av = aG(av, au, at, aw, ar[aB + 10], af, 3200236656),
      aw = aG(aw, av, au, at, ar[aB + 13], ai, 681279174),
      at = aG(at, aw, av, au, ar[aB + 0], ah, 3936430074),
      au = aG(au, at, aw, av, ar[aB + 3], ag, 3572445317),
      av = aG(av, au, at, aw, ar[aB + 6], af, 76029189),
      aw = aG(aw, av, au, at, ar[aB + 9], ai, 3654602809),
      at = aG(at, aw, av, au, ar[aB + 12], ah, 3873151461),
      au = aG(au, at, aw, av, ar[aB + 15], ag, 530742520),
      av = aG(av, au, at, aw, ar[aB + 2], af, 3299628645),
      aw = aF(aw, av, au, at, ar[aB + 0], ae, 4096336452),
      at = aF(at, aw, av, au, ar[aB + 7], ad, 1126891415),
      au = aF(au, at, aw, av, ar[aB + 14], ac, 2878612391),
      av = aF(av, au, at, aw, ar[aB + 5], ab, 4237533241),
      aw = aF(aw, av, au, at, ar[aB + 12], ae, 1700485571),
      at = aF(at, aw, av, au, ar[aB + 3], ad, 2399980690),
      au = aF(au, at, aw, av, ar[aB + 10], ac, 4293915773),
      av = aF(av, au, at, aw, ar[aB + 1], ab, 2240044497),
      aw = aF(aw, av, au, at, ar[aB + 8], ae, 1873313359),
      at = aF(at, aw, av, au, ar[aB + 15], ad, 4264355552),
      au = aF(au, at, aw, av, ar[aB + 6], ac, 2734768916),
      av = aF(av, au, at, aw, ar[aB + 13], ab, 1309151649),
      aw = aF(aw, av, au, at, ar[aB + 4], ae, 4149444226),
      at = aF(at, aw, av, au, ar[aB + 11], ad, 3174756917),
      au = aF(au, at, aw, av, ar[aB + 2], ac, 718787259),
      av = aF(av, au, at, aw, ar[aB + 9], ab, 3951481745),
      aw = aN(aw, aA),
      av = aN(av, az),
      au = aN(au, ay),
      at = aN(at, ax)
  }
  var aa = aD(aw) + aD(av) + aD(au) + aD(at);
  return aa.toLowerCase()
}

